﻿using System;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;


namespace ApplicationGenerator.DataAccess
{
    public class Serializer
    {
        #region Variables
        #endregion

        #region Construction

        public Serializer()
        {

        }

        #endregion

        #region Serialization and Deserialization methods ...

        public static bool Serialize(DataAccess.Policy policy, object objToSerialize, Guid formGuid, string title)
        {
            Stream tmpStream = new MemoryStream();

            // Create binary formatter to serialize the data
            BinaryFormatter binaryFormatter = new BinaryFormatter();

            // Serialize the data
            binaryFormatter.Serialize(tmpStream, objToSerialize);

            // Flush and close the filestream
            tmpStream.Flush();

            //reset pointer
            tmpStream.Position = 0;

            //copy bin data to byte array
            byte[] bytes = new byte[tmpStream.Length];
            tmpStream.Read(bytes, 0, bytes.Length);

            //now close stream
            tmpStream.Close();

            //and write data to database 
            return DataAccess.DesignerDataBaseManager.Instance.SaveToDatabase(policy, bytes, formGuid, title);
            //return DataAccess.DesignerDataBaseManager.Instance.SaveFormToDatabase(bytes, formGuid, title);
        }

        public static bool Update(DataAccess.Policy policy, object objToBeUpdated, string title)
        {
            Stream tmpStream = new MemoryStream();

            // Create binary formatter to serialise the data
            BinaryFormatter binaryFormatter = new BinaryFormatter();

            // Serialize the data
            binaryFormatter.Serialize(tmpStream, objToBeUpdated);

            // Flush and close the filestream
            tmpStream.Flush();

            //reset pointer
            tmpStream.Position = 0;

            //copy bin data to byte array
            byte[] bytes = new byte[tmpStream.Length];
            tmpStream.Read(bytes, 0, bytes.Length);

            //now close stream
            tmpStream.Close();

            //and write data to database 
            return DataAccess.DesignerDataBaseManager.Instance.UpdateTable(policy, title, bytes);
        }

        public static object Deserialize(DataAccess.Policy policy, Guid id)
        {
            // Create an object to return
            object result = null;

            byte[] bytes = DataAccess.DesignerDataBaseManager.Instance.LoadFromDatabase(policy, id);

            if (null == bytes)
            {
                return null;
            }

            // Create a filestream and a binary formatter
            Stream tmpStream = new MemoryStream(bytes);

            BinaryFormatter binaryFormatter = new BinaryFormatter();

            // Deserialize into the object
            result = binaryFormatter.Deserialize(tmpStream);

            // Close the streams
            if (null != tmpStream)
            {
                tmpStream.Close();
            }

            return result;
        }

        public static object Deserialize(DataAccess.Policy policy, string title)
        {
            // Create an object to return
            object result = null;

            byte[] bytes = DataAccess.DesignerDataBaseManager.Instance.LoadFromDatabase(policy, title);

            if (null == bytes)
            {
                return null;
            }

            // Create a filestream and a binary formatter
            Stream tmpStream = new MemoryStream(bytes);

            BinaryFormatter binaryFormatter = new BinaryFormatter();

            // Deserialize into the object
            result = binaryFormatter.Deserialize(tmpStream);

            // Close the streams
            if (null != tmpStream)
            {
                tmpStream.Close();
            }

            return result;
        }

        #endregion
    }
}
